package com.ganhuoshitang.multipledatasources.controller;

import com.ganhuoshitang.multipledatasources.config.DataSourceUtil;
import com.ganhuoshitang.multipledatasources.entity.Account;
import com.ganhuoshitang.multipledatasources.entity.Orders;
import com.ganhuoshitang.multipledatasources.mapper.AccountMapper;
import com.ganhuoshitang.multipledatasources.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author: 公众号:干货食堂
 * Project Name: multiple-data-sources
 * File Name: TestController
 * Package Name: com.ganhuoshitang.multipledatasources.controller
 * Date: 2022/6/7 22:33
 * Copyright (c) 2022,All Rights Reserved.
 * @Description
 */
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    OrdersMapper ordersMapper;

    @GetMapping("addAccount")
    public Account addAccount(){
        Account account = new Account();
        account.setUserId(1);
        account.setUpdateTime(new Date());
        account.setBalance(new BigDecimal(1));
        accountMapper.insert(account);
        return account;
    }

    @GetMapping("addOrder")
    public Orders addOrder(){
        DataSourceUtil.setDB("db2");

        Orders orders = new Orders();
        orders.setUserId(1);
        orders.setUpdateTime(new Date());
        orders.setAddTime(new Date());
        orders.setPayAmount(new BigDecimal(1));
        orders.setProductId(1);
        ordersMapper.insert(orders);
        return orders;
    }
}
